# encoding    : utf-8
# author      : 伦裕如
# project     : hrjt_-py1
# file        : test1
# time        : 2024-06-19 下午2:48 

# 导包
import pymysql


def main():
    try:
        # 数据库连接
        db = pymysql.connect(
            host = 'localhost',
            port = 3306,
            database = 'jiangsu',
            user = 'root',
            password = 'root',
            charset = 'utf8'
        )
        # 创建游标
        with db.cursor() as cursor:
            # 编写 sql
            sql = "select * from student where s_id = '01'"
            # 执行sql
            cursor.execute(sql)
            # 获取返回数据
            data = cursor.fetchall()
            print(data)

            # 编写 sql 增
            value = "('11','你走开', '2001-12-12', '女'),('12','你不要过来啊', '2001-12-12', '女'),('13','我四给你看哈', '2001-12-12', '女')"
            sql2 = f"insert into student values {value}"
            cursor.execute(sql2)
            # 增删改操作需要提交
            db.commit()
    except pymysql.err.OperationalError as oe:
        print(f'数据库连接异常，检查配置信息是否正确: {oe}')
    except pymysql.err.ProgrammingError as pe:
        print("SQL语句错误")
    except pymysql.err.DataError as de:
        print(f'数据异常: {de}')
    except Exception as e:
        print(f'未知异常: {e}')
    finally:
        print('执行完成')


if __name__ == '__main__':
    main()
